package com.yt.fras.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yt.fras.mapper.AttendanceMapper;
import com.yt.fras.mapper.ManagementGroupMapper;
import com.yt.fras.mapper.UserMapper;
import com.yt.fras.pojo.entity.Attendance;
import com.yt.fras.service.MailService;
import com.yt.fras.service.ScheduledService;
import com.yt.fras.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static com.yt.fras.constant.AttendanceStatus.*;

/**
 * ClassName:ScheduledServiceImpl
 * Package:com.yt.fras.service.impl
 * Description:
 *
 * @date:2021/11/12 16:46
 * @author:yt
 */
@Service
@Slf4j
public class ScheduledServiceImpl implements ScheduledService {

    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private MailService mailService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ManagementGroupMapper managementGroupMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    @Scheduled(cron = "0 0/1 * * * ?")
    @Async("taskExecutor")
    public void judgeAttendancesStatus() {

        QueryWrapper<Attendance> wrapper = new QueryWrapper<>();
        wrapper.ne("status", 0);
        List<Attendance> attendances = attendanceMapper.selectList(wrapper);
        if (attendances != null) {
            Date current = new Date();
            attendances.forEach(attendance -> {
                if (attendance.getStatus() == WAITING) {
                    if (current.getTime() > attendance.getStartTime().getTime() && current.getTime() < attendance.getEndTime().getTime()) {
                        attendance.setStatus(RUNNING);
                        attendanceMapper.updateById(attendance);
                        //List<UserInfo> userInfoList = userMapper.getUserInfoListByGroup(attendance.getGroupId());
                        //ManagementGroup managementGroup = managementGroupMapper.selectById(attendance.getGroupId());
                        //SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        //userInfoList.forEach(userInfo -> mailService.sendHtmlMail(userInfo.getEmail(), "签到通知", SetContent.remindStart(userInfo.getUsername(), managementGroup.getName(), simpleDateFormat.format(attendance.getStartTime()), attendance.getDescription())));
                        log.info(attendance.getId() + ":考勤状态由待考勤变为正在考勤");
                    } else if (current.getTime() > attendance.getEndTime().getTime()) {
                        attendance.setStatus(ENDING);
                        attendanceMapper.updateById(attendance);
                        redisUtil.hDel(attendance.getId(), "latitude", "longitude");
                        log.info(attendance.getId() + ":考勤状态由待考勤变为考勤结束");
                    }
                } else if (attendance.getStatus() == RUNNING) {
                    if (current.getTime() > attendance.getEndTime().getTime()) {
                        attendance.setStatus(ENDING);
                        attendanceMapper.updateById(attendance);
                        redisUtil.hDel(attendance.getId(), "latitude", "longitude");
                        log.info(attendance.getId() + ":考勤状态由正在考勤变为考勤结束");
                    }
                }
            });
        }
    }

}
